package com.bacon.gmall.web.controller;

import com.bacon.gmall.cart.client.CartFeignClient;
import com.bacon.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * @author： bacon、
 * @version： 1.0
 * @create： 2021-01-04 11:00
 * @description:
 */
@Controller
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient;

    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes) {
        cartFeignClient.addToCart(skuId,skuNum);
        redirectAttributes.addAttribute("skuId",skuId);
        redirectAttributes.addAttribute("skuNum",skuNum);

        return "redirect:http://www.gmall.com/addToCart.html";
    }

    @GetMapping("/addToCart.html")
    public String addToCart(Long skuId, Integer skuNum, Model model) {

        CartInfo cartInfo = cartFeignClient.findCartInfoBySkuId(skuId);
        cartInfo.setSkuNum(skuNum);
        model.addAttribute("cartInfo",cartInfo);
        return "cart/addCart";
    }

    @GetMapping("/cart.html")
    public String cart() {
        return "cart/index";
    }
}
